package com.fz.song.mcos.vo.userdetails;

import com.fz.song.mcos.entity.McUser;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Collection;

public class MyUserDetails implements UserDetails {

    private McUser mcUser;

    public MyUserDetails(McUser mcUser) {
        super();
        this.mcUser = mcUser;
    }

    public Collection<? extends GrantedAuthority> getAuthorities() {
        return AuthorityUtils.commaSeparatedStringToAuthorityList(mcUser.getRole().toLowerCase() + ",ROLE_" + mcUser.getRole().toLowerCase());
    }

    public String getPassword() {
        return mcUser.getPassword();
    }

    public String getUsername() {
        return mcUser.getUserName();
    }

    /**
     * 判断账户是否过期
     *
     * @return
     */
    public boolean isAccountNonExpired() {
        int state = mcUser.getState();
        if (state == 3) {
            return false;
        }
        return true;
    }

    /**
     * 账户是否没有被锁定
     *
     * @return
     */
    public boolean isAccountNonLocked() {
        int state = mcUser.getState();
        if (state == 2) {
            return false;
        }
        return true;
    }

    /**
     * 登录凭证是否已经过期
     *
     * @return
     */
    public boolean isCredentialsNonExpired() {
        return true;
    }

    /**
     * 账号是否是开启的
     *
     * @return
     */
    public boolean isEnabled() {
        int state = mcUser.getState();
        if (state == 0 || state == 1) {
            return true;
        }
        return false;
    }
}
